package com.ssm.controller;

import com.ssm.pojo.UserInformation;
import com.ssm.service.EmailCode;
import com.ssm.service.UserAdd;
//import com.sun.org.apache.xpath.internal.operations.String;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员接口
 * */
@Controller
public class addUser {

    @Resource(name="userAddImp")
    private UserAdd userAddImp;
    @Resource(name="emailCodeImp")
    private EmailCode emailCodeImp;
    @Autowired
    HttpServletRequest request;
    /**
     * 发送邮箱验证码
     * @return map
     */
    @RequestMapping(value="/sendEmailCode")
    @ResponseBody
    public Map getUser(String email) throws UnsupportedEncodingException, MessagingException, FileNotFoundException {
        //  state 0,成功，1、失败，2、邮箱已经注册，3、发送超过5条,4、邮箱参数为空，5、邮箱格式不正确
        Map<Object, Object> addUserMes = new HashMap<>();
        if(email != null){
            if(email.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*")){
                List<UserInformation> emailList = userAddImp.selectEmail(email);
                if(emailList.size() != 0){
                    addUserMes.put("state",2);
                    return addUserMes;
                }
                HttpSession session = request.getSession();
                Object emailTime = session.getAttribute("emailTime");
                if(!session.isNew()){
                    if(emailTime != null){
                        if((int)emailTime >= 5){
                            addUserMes.put("state",3);
                            return addUserMes;
                        }
                    }
                }
                try{
                    String emailCode = emailCodeImp.registeredEmailCode(new String[]{email});
                    session.setAttribute("emailCode",emailCode);
                    session.setAttribute("emailTime",emailTime == null ? 1 : (int)emailTime + 1);
                    addUserMes.put("state",0);
                }catch (Exception e){
                    addUserMes.put("state",1);
                }
            }else{
                addUserMes.put("state",5);
            }
        }else{
            addUserMes.put("state",4);
        }
        return addUserMes;
    }
    /**
     * 注册
     *
     * */
    @RequestMapping(value="/gain")
    @ResponseBody
    public Map gainUser(String user,String pwd,String code){
        //  state 0,成功，1、失败，2、账号已经注册，3、验证码失效,4、参数为空，5、账号或者密码格式不正确，6、验证码错误
        HashMap<Object, Object> gainUser = new HashMap<>();

        Object emailCode = request.getSession().getAttribute("emailCode");


        if(emailCode == null){
            gainUser.put("type",3);
        }else{
            if(user != null && pwd != null && code != null){
                if(((String)emailCode).equals(code)){
                    if(user.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$") && pwd.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$")){
                        try{
                            UserInformation userInformation = new UserInformation();
                            userInformation.setUserId(user);
                            userInformation.setPassWord(pwd);
                            userAddImp.userAdd(userInformation);
                            gainUser.put("type",0);
                        }catch (Exception e){
                            gainUser.put("type",2);
                        }
                    }else{
                        gainUser.put("type",5);
                    }
                }else{
                    gainUser.put("type",6);
                }
            }else{
                gainUser.put("type",4);
            }
        }
        return gainUser;
    }
}
